package br.com.octobite.cashkeeper.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import br.com.octobite.cashkeeper.CashKeeperActivity;
import br.com.octobite.cashkeeper.R;
import br.com.octobite.cashkeeper.dao.CategoriaDAO;
import br.com.octobite.cashkeeper.model.Categoria;

public class AddCategoriaDialog extends DialogFragment {

	DialogListener dListener;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			dListener = (DialogListener) activity;
		} catch (ClassCastException e) {
			Log.i(CashKeeperActivity.TAG, "Deve ser DialogListener");
		}
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {

		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		LayoutInflater inflater = getActivity().getLayoutInflater();
		View dialogView = inflater.inflate(
				R.layout.add_categoria_dialog_layout, null);
		final TextView tView = (TextView) dialogView
				.findViewById(R.id.txt_categoria);
		builder.setView(dialogView)
				.setMessage("Gravar categoria")
				// Add action buttons
				.setPositiveButton("Gravar",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int id) {
								dListener.salvaCategoria(tView.getText()
										.toString());
							}
						})
				.setNegativeButton("Cancelar",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								AddCategoriaDialog.this.getDialog().cancel();
							}
						});
		return builder.create();

	}

}
